import { listBottomNav } from '@/api/common'
import { setNavList } from '@/utils/auth'

export function navList(enterpriseId) {
  let navData = []
  let params = {
    enterpriseId: enterpriseId
  }
  listBottomNav(params).then((response) => {
    if (response.isError) return
    let dataInfo = response.data
    if (response.data.length) {
      dataInfo.forEach(item => {
        switch (item.linkType) {
          default:
          case 1005:
            item.pagePath = 'pages/home/main' // 首页
            break
          case 1006:
            item.pagePath = 'pages/shop/main' // 分类
            break
          case 1007:
            item.pagePath = 'pages/shop/cart/main' // 购物车
            break
          case 1008:
            item.pagePath = 'pages/personal/main' // 我的
            break
          case 1004:
            item.pagePath = 'pages/shop/main' // 分类链接
            break
          case 2001:
            item.pagePath = 'pages/shop/detail/main' // 商品详情
            break
          case 1101:
            item.pagePath = 'pages/home/second-custom/main' // 自定义页面
            break
          case 1401:
            item.pagePath = 'pages/home/live-list/main' // 直播列表
            break
          case 1001:
            item.pagePath = 'pages/personal/coupon/coupon-center/main' // 领券中心
            break
          case 1009:
            item.pagePath = 'pages/home/content-center/main' // 内容中心
            break
          case 4001:
            item.pagePath = 'pages/home/content-center/content-detail/main' // 内容中心详情
            break
          case 1010:
            item.pagePath = 'pages/shop/all-goods/main' // 全部商品
            break
          case 1501:
            item.pagePath = 'pages/home/default-seckill/main' // 限时秒杀
            break
        }
        if (item.linkParams) {
          // 解析linkParams参数开始
          let arr = item.linkParams.split('?')
          let params = arr[1].split('&')
          let obj = {}
          for (let i = 0; i < params.length; i++) {
            let param = params[i].split('=')
            obj[param[0]] = param[1]
          }
          // 解析linkParams参数结束
          item.linkObj = obj
          navData.push({
            pagePath: item.pagePath,
            linkType: item.linkType,
            linkParams: item.linkParams,
            linkObj: item.linkObj
          })
        } else {
          navData.push({
            pagePath: item.pagePath,
            linkType: item.linkType
          })
        }
      })
      setNavList(navData)
    }
  }).catch(() => {
  })
  return navData
}
